<?php

namespace app\merchant\controller;

use app\merchant\model\Goods as GoodsModel;
use app\merchant\model\Category as CategoryModel;
use app\merchant\model\Wxapp as WxappModel;
use app\merchant\service\Goods as GoodsService;

/**
 * 商品管理类
 * Class Index
 * @package app\merchant\controller
 */
class Goods extends Base
{

    /**
     * 商品分类
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function goodsCategory(){

        // 商品分类
        $catgory = CategoryModel::getCacheTree();
        return $this->renderSuccess( compact('catgory'),'查询成功');
    }



    /**
     * 商品列表
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {

        // 获取全部商品列表
        $model = new GoodsModel;
        $list = $model->getMerchantList($this->request->param());

        // 商品分类
        $catgory = CategoryModel::getCacheTree();
        return $this->renderSuccess( compact('list', 'catgory'));
    }


    /**
     * 获取商品添加页面所需的数据
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function getGoodsData()
    {
        $wxapp_id = $this->getWxappId();
        //获取分销方案
        $dealerCase = WxappModel::dealerCase($wxapp_id);
        $goodsData = GoodsService::getEditData(null, 'add');

        return $this->renderSuccess( compact('dealerCase', 'goodsData'));

    }


    /**
     * 商品添加
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function add()
    {

        $model = new GoodsModel;

        // 新增记录
        if ($model->add($this->postData())) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$model->getError() ?: '操作失败');

    }


    /**
     * 获取商品详情
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();

        if(!isset($param['goods_id']) || empty($param['goods_id'])){
            return $this->renderError([],"参数错误");
        }
        $wxapp_id = $this->getWxappId();
        //获取分销方案
        $dealerCase = WxappModel::dealerCase($wxapp_id);
        // 商品详情
        $detail = GoodsModel::detail($param['goods_id']);
        $goodsData = GoodsService::getEditData($detail);

        return $this->renderSuccess( compact('detail', 'goodsData','dealerCase'),'查询成功');


    }


    /**
     * 商品编辑
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = $this->postData();

        if(!isset($param['goods_id']) || empty($param['goods_id'])){
            return $this->renderError([],"参数错误");
        }
        // 商品详情
        $model = GoodsModel::detail($param['goods_id']);
        if(!$model){
            return $this->renderError([],"商品不存在");
        }

        // 新增记录
        if ($model->edit($param)) {
            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError([],$model->getError() ?: '操作失败');

    }


    /**
     * 修改商品状态
     * @param $goods_id
     * @param boolean $state 商品状态(10上架 20下架)
     * @return array
     */
    public function state()
    {
        $param = $this->postData();

        if(!isset($param['goods_id']) || empty($param['goods_id'])){
            return $this->renderError([],"参数错误");
        }
        // 商品详情
        $model = GoodsModel::detail($param['goods_id']);
        if(!$model){
            return $this->renderError([],"商品不存在");
        }

        if (!$model->setStatus($param['state'])) {
            return $this->renderError([],'操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }

    /**
     * 删除商品
     * @param $goods_id
     * @return array
     */
    public function delete()
    {
        $param = $this->postData();

        if(!isset($param['goods_id']) || empty($param['goods_id'])){
            return $this->renderError([],"参数错误");
        }
        // 商品详情
        $model = GoodsModel::detail($param['goods_id']);
        if(!$model){
            return $this->renderError([],"商品不存在");
        }

        if (!$model->setDelete()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }
    


}
